package cn.yuhao.goodbook.user.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;
import cn.yuhao.goodbook.user.domain.User;
import cn.yuhao.goodbook.user.service.UserService;


public class UserServlet extends BaseServlet {
	private UserService userService = new UserService();

	public String ajaxValidateLoginname(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String loginname = request.getParameter("loginname");
		boolean b = userService.ajaxValidateLoginname(loginname);
		response.getWriter().print(b);
		return null;
	}

	public String ajaxValidateEmail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String email = request.getParameter("email");
		boolean b = userService.ajaxValidateEmail(email);
		response.getWriter().print(b);
		return null;
	}

	public String ajaxValidateVerifyCode(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String verifyCode = request.getParameter("verifyCode");
		String vcode = (String) request.getSession().getAttribute("vCode");
		boolean b = verifyCode.equalsIgnoreCase(vcode);
		response.getWriter().print(b);
		return null;
	}

	public String regist(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("aaaaaaa");
		//封装表单
		User formuser =CommonUtils.toBean(request.getParameterMap(), User.class);
		//校验表单
		Map<String, String> errors=validateRegist(formuser, request.getSession());
		if(errors.size()>0){
			//保存错误信息，转发到msg.jsp显示
			request.setAttribute("errors", errors);
			request.setAttribute("form", formuser);
			return "f:/jsps/user/regist.jsp";
		}
		
		//使用service完成业务
		userService.regist(formuser);
		request.setAttribute("code", "success");
		request.setAttribute("msg", "注册成功，请马上到邮箱激活帐户");
		return "f:/jsps/msg.jsp";
	}
	
	private Map<String, String> validateRegist(User user,HttpSession session){
		Map<String, String> errors=new HashMap<String, String>();
		/*
		 * 用户名校验
		 */
		if(user.getLoginname()==null||user.getLoginname().trim()==null){
			errors.put("loginname", "用户名不能为空");
		}else if(user.getLoginname().length()<3||user.getLoginname().length()>20){
			errors.put("loginname", "用户名长度为3~20个字符");
		}else if(!userService.ajaxValidateLoginname(user.getLoginname())){
			errors.put("loginname", "用户名已被注册");
		}
		/*
		 * 登录密码校验
		 */
		if(user.getLoginpass()==null||user.getLoginpass().trim()==null){
			errors.put("loginpass", "密码不能为空");
		}else if(user.getLoginpass().length()<3||user.getLoginpass().length()>20){
			errors.put("loginpass", "密码长度为3~20个字符");
		}
		/*
		 * 确认密码校验
		 */
		if(user.getReloginpass()==null||user.getReloginpass().trim()==null){
			errors.put("reloginpass", "密码不能为空");
		}else if(user.getReloginpass().length()<3||user.getReloginpass().length()>20){
			errors.put("reloginpass", "密码长度为3~20个字符");
		}else if(!user.getLoginpass().equals(user.getReloginpass())){
			errors.put("reloginpass", "密码不一致");
		}
		/*
		 * email校验
		 */
		if(user.getEmail()==null||user.getEmail().trim()==null){
			errors.put("email", "Email不能为空");
		}else if(!userService.ajaxValidateEmail(user.getEmail())){
			errors.put("email", "Email已被注册");
		}else if(!user.getEmail().matches("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\\.[a-zA-Z0-9_-]{2,3}){1,2})$")) {
			errors.put("email", "Email格式错误！");
		}
		/*
		 * 验证码校验
		 */
		String vcode=(String) session.getAttribute("vCode");
		String verifyCode=user.getVerifyCode();
		if(!vcode.equalsIgnoreCase(verifyCode)){
			errors.put("verifyCode", "验证码错误");
		}else if(verifyCode==null||verifyCode.trim()==null){
			errors.put("verifyCode","验证码不能为空");
		}
		return errors;
		
	}
}
